﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HTDAL.HTSPC_DBDataSetTableAdapters;
using System.Data;
using HTspcAppProject.Model;
using System.IO;

namespace HTspcAppProject
{
    public class InitlClass
    {
        public InitlClass()
        {
            DevInfoInit("0");
            DicInit();
            CommonClass.devCount = CommonClass.devDtList.Rows.Count;
            CommonClass.DelErrLog();
        }

        /// <summary>
        /// 字典初始化
        /// </summary>
        private void DicInit()
        {
            //测量参数初始化
            CommonClass.pmDict = new Dictionary<string, HTspcAppProject.Model.ht_pmmr>();
            foreach (DataRow dr in CommonClass.devDtList.Rows)
            {
                string devNm = Convert.ToString(dr["devNm"]);
                ht_pmmr pmmr = new ht_pmmr();
                pmmr.devId = Convert.ToInt32(dr["devId"]);
                pmmr.devNm = Convert.ToString(dr["devNm"]);
                pmmr.gyid = Convert.ToString(dr["gyid"]);
                pmmr.hswdSet = Convert.ToDouble(dr["hswdSet"]);
                pmmr.ycwdSer = Convert.ToDouble(dr["ycwdSer"]);
                pmmr.cpSet = Convert.ToDouble(dr["cpSet"]);
                CommonClass.pmDict.Add(devNm, pmmr);
            }

            //状态参数初始化
            CommonClass.stDict = new Dictionary<string, HTspcAppProject.Model.ht_stmr>();
            ht_stmrTableAdapter stmrTa = new ht_stmrTableAdapter();
            DataTable stmrDt = stmrTa.GetData();
            foreach (DataRow dr in stmrDt.Rows)
            {
                ht_stmr stmr = new ht_stmr();
                stmr.devId = Convert.ToInt32(dr["devId"]);
                stmr.devNm = Convert.ToString(dr["devNm"]);
                stmr.powerSt = Convert.ToString(dr["powerSt"]);
                stmr.onRt = Convert.ToDouble(dr["onRt"]);
                stmr.offRt = Convert.ToDouble(dr["offRt"]);
                stmr.OEE = Convert.ToDouble(dr["OEE"]);
                stmr.prdbn = Convert.ToInt32(dr["prdbn"]);
                stmr.chktms = Convert.ToInt32(dr["chktms"]);
                stmr.dt = Convert.ToDateTime(dr["dt"]);
                CommonClass.stDict.Add(stmr.devNm, stmr);
            }

            //寄存器通道字典初始化
            ht_devCon_TCPTableAdapter TcpConTa = new ht_devCon_TCPTableAdapter();
            DataTable TcpConDt = TcpConTa.GetData();
            CommonClass.chDict = new Dictionary<int, string[]>();
            foreach (DataRow dr in TcpConDt.Rows)
            {
                int chNo = Convert.ToInt32(dr["chNo"]);
                string devNm = Convert.ToString(dr["devNm"]);
                string paramNm = Convert.ToString(dr["pmName"]);
                CommonClass.chDict.Add(chNo, new string[] { devNm, paramNm });
            }
        }

        public static void DevInfoInit(object msg)
        {
            lock (CommonClass.devDtList)
            {
                ht_devSetTableAdapter devSetTa = new ht_devSetTableAdapter();
                CommonClass.devDtList = devSetTa.GetData();
            }

        }


    }
}
